package com.meitianhui.convenitenbuy.module.home.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.home.contract.HomeCollectionContract;
import com.meitianhui.convenitenbuy.module.home.http.HomeApiDataSource;
import com.meitianhui.convenitenbuy.module.home.model.bean.OfficialStore;
import com.meitianhui.convenitenbuy.module.home.ui.activity.HomeCollectionActivity;
import com.meitianhui.convenitenbuy.module.mine.http.MineApiDataSource;
import com.meitianhui.convenitenbuy.module.mine.model.CollectedStoresBean;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;

import java.util.HashMap;
import java.util.Map;

public class HomeCollectionPresenter implements HomeCollectionContract.Presenter {

    private HomeCollectionActivity mContext;
    private HomeCollectionContract.View mView;

    public HomeCollectionPresenter(HomeCollectionContract.View view,
                                   HomeCollectionActivity context) {
        this.mView = view;
        this.mContext = context;
    }

    @Override
    public void getCollection() {
        Map<String, String> map = new HashMap<>();
        map.put("consumer_id", InfoProvider.getUserToken(mContext).getMember_id());

        MineApiDataSource.getInstance()
                .getUserCollectionStore(InfoProvider.getAppToken(mContext),
                        "stores.consumer.favoriteStoreList", new Gson().toJson(map))
                .subscribe(new ResponseObserver<BaseResponse<CollectedStoresBean>>(mContext, true) {
                    @Override
                    public void onSuccess(BaseResponse<CollectedStoresBean> response) {
                        mView.getCollectionSuccess(response.data);
                    }
                });
    }

    @Override
    public void officialStorePost(String storeId, String storeName,
                                  String goodsCode, String goodsUrl) {
        Map<String, String> params = new HashMap<>();
        params.put("goods_code", goodsCode);
        params.put("goods_url", goodsUrl);
        params.put("mobile", InfoProvider.getUserInfo(App.getInstance()).getMobile());
        params.put("nick_name", InfoProvider.getConsumerInfo(App.getInstance()).getNick_name());
        params.put("member_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        params.put("stores_name", storeName);
        params.put("recommend_stores_id", storeId);
        String json = new Gson().toJson(params);

        HomeApiDataSource.getInstance()
                .officialStorePost(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "order.consumerFreeGetRecordCreate", json)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        mView.officialStorePostSuccess(response.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        mView.officialStorePostFail(code, msg);
                    }
                });
    }

    @Override
    public void settingDefaultStore(String storeId) {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("stores_id", storeId);
        hashMap.put("consumer_id", InfoProvider.getUserToken(App.getInstance()).getMember_id());
        hashMap.put("is_llm_stores", "Y");
        String json = new Gson().toJson(hashMap);

        HomeApiDataSource.getInstance()
                .settingDefaultStore(InfoProvider.getUserToken(App.getInstance()).getUser_token(),
                        "stores.consumer.favoriteStore", json)
                .subscribe(new ResponseObserver<BaseResponse<OfficialStore>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<OfficialStore> response) {
                        mView.settingDefaultStoreSuccess();
                    }
                });
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
